package com.scope.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.scope.R;
import com.scope.customViews.CustomView;
import com.scope.customViews.GalleryExplorer;
import com.scope.customViews.ImagePager;
import com.scope.tasks.ZoomAnimator;

import java.io.File;

public class GalleryActivity extends CustomActivity {
    public final static String SELECTED_ITEM = "SI";
    public final static String SLIDER_VSB = "SV";

    protected int mAccent;
    public int getAccent() {
        return mAccent;
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.gallery, menu);
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString(CURR_PATH, mGalleryExplorer.getPath());
        outState.putInt(SELECTED_ITEM, mGalleryExplorer.getCheckedItemPosition());
        outState.putBoolean(SLIDER_VSB, mImageSliderVisible);
        super.onSaveInstanceState(outState);
    }

    protected void initializeViews(Bundle savedInstanceState) {
        initializeLayout();
        initializeGalleryExplorer(savedInstanceState);
        initializeExpandedImageView();
        initializeViewAnimator();
    }

    protected void positionViews() {}

    private FrameLayout mLayout;
    private void initializeLayout() {
        mLayout = new FrameLayout(getApplicationContext());
        ViewGroup.LayoutParams lp =
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(mLayout, lp);
    }


    private GalleryExplorer mGalleryExplorer;
    private void initializeGalleryExplorer(Bundle savedInstanceState) {
        mGalleryExplorer = new GalleryExplorer(getApplicationContext());
        mGalleryExplorer.setAccent(getResources().getColor(R.color.Green));
        FrameLayout.LayoutParams lp =
                new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);

        if (savedInstanceState != null) {
            String str = savedInstanceState.getString(CURR_PATH);
            int pos = savedInstanceState.getInt(SELECTED_ITEM);
            mImageSliderVisible = savedInstanceState.getBoolean(SLIDER_VSB);
            if (str != null && str.length() != 0) {
                mGalleryExplorer.setPath(str);
                View view = mGalleryExplorer.selectFileAtPosition(pos);
                if (view != null && mImageSliderVisible == true) {
                    view.performClick();
                }
            }
        }
        mLayout.addView(mGalleryExplorer, lp);

        mGalleryExplorer.setOnFileClickLister(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mImageSliderVisible = true;
                mImageSlider.setCurrentItem(position);
                mZoomAnimator.setStartView(view, ZoomAnimator.ViewState.FIX);
                mZoomAnimator.animate();
            }
        });

        mGalleryExplorer.setOnFolderClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mGalleryExplorer.setPath((String)mGalleryExplorer.getItemAtPosition(position));
                mImageSlider.setImagePaths(mGalleryExplorer.getFilePaths());
            }
        });
    }

    private Boolean mImageSliderVisible = false;
    private final File ROOT_FILE = Environment.getExternalStorageDirectory().getParentFile();

    private ImagePager mImageSlider;
    private void initializeExpandedImageView() {
        mImageSlider = new ImagePager(this);
        mLayout.addView(mImageSlider);
        mImageSlider.setImagePaths(mGalleryExplorer.getFilePaths());

        mImageSlider.setVisibility(View.GONE);
        mImageSlider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = mGalleryExplorer.selectFileAtPosition(mImageSlider.getCurrentItem());
                mZoomAnimator.setEndView(view, ZoomAnimator.ViewState.FIX);
                mZoomAnimator.animateRev();
                mImageSliderVisible = false;
            }
        });

        mImageSlider.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                mGalleryExplorer.selectFileAtPosition(mImageSlider.getCurrentItem());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    private ZoomAnimator mZoomAnimator;
    private void initializeViewAnimator() {
        mZoomAnimator = new ZoomAnimator(getApplicationContext());
        mZoomAnimator.setStartView(mGalleryExplorer, ZoomAnimator.ViewState.FIX);
        mZoomAnimator.setMainView(mImageSlider, ZoomAnimator.ViewState.GONE);
        mZoomAnimator.setFrame(mLayout);
        mZoomAnimator.animate();
        mZoomAnimator.animateRev();
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_translate:
                if (mGalleryExplorer.getCheckedItemPosition() != AdapterView.INVALID_POSITION) {
                    Intent intent = new Intent(GalleryActivity.this, PreprocessActivity.class);
                    intent.putExtra(
                            SELECTED_PATH,
                            (String)mGalleryExplorer.getItemAtPosition(mGalleryExplorer.getCheckedItemPosition()));
                    startActivity(intent);
                }
                return true;
            default:
                return super.onMenuItemSelected(featureId, item);
        }
    }

    public void setAccent(int accent) {
        mAccent = accent;
        CustomView.StaticMethods.setAccent(mGalleryExplorer, accent);
    }

    @Override
    public void onBackPressed() {
        if (mImageSliderVisible) {
            mImageSlider.performClick();
        } else {
            File currFile = new File(mGalleryExplorer.getPath());
            if (currFile.compareTo(ROOT_FILE) != 0) {
                mGalleryExplorer.setPath(currFile.getParent());
            } else {
                super.onBackPressed();
            }
        }
    }
}
